#include "../kore2.h"

/**
 * Attach the context as the primary context. This should wake the context
 * up and cause it to initialize and be ready for its rendering work.
 */
int kore::sdl::SdlContext::attachPrimaryContext()
{
	printf("[sdlcontext] Attaching context (%dx%d at %d bpp)\n", this->m_width, this->m_height, this->m_bpp);

	Uint32 flags;
	flags = SDL_HWSURFACE | SDL_DOUBLEBUF;
	if (this->m_isFullScreen)
	{
		flags |= SDL_FULLSCREEN;
	}

	if (SDL_SetVideoMode(this->m_width, this->m_height, this->m_bpp, flags) == NULL)
	{
		printf("[sdlcontext] Failed to initialize desired video mode!\n");
		exit(1);
	}

}

/**
 * Detach the context from being the primary context. This should make the
 * context hibernate and close everything it has open relating to graphics.
 */
int kore::sdl::SdlContext::detachPrimaryContext()
{
	printf("[sdlcontext] Detaching context\n");
	SDL_FreeSurface(SDL_GetVideoSurface());

}

/**
 * @brief Toggle full screen mode
 *
 * This method will do what is neccessary to toggle fullscreen mode for the
 * current platform.
 *
 * @param bool If true, the application will run in full screen.
 */
void kore::sdl::SdlContext::setFullscreen(bool fullscreen)
{
	// Toggle fullscreen mode and reload what is needed
	this->m_isFullScreen = fullscreen;
}

/**
 * @brief Set context parameters
 *
 * Defines the width, height and bits per pixel for the context.
 */
void kore::sdl::SdlContext::setParameters(Uint16 width, Uint16 height, Uint16 bpp)
{
	// Set parameters for context
	this->m_width = width;
	this->m_height = height;
	this->m_bpp = bpp;
}

void kore::sdl::SdlContext::refresh()
{
	SDL_Flip(SDL_GetVideoSurface());
}